package com.face.receive.jsu;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;

import org.apache.tomcat.util.http.fileupload.FileItem;
import org.apache.tomcat.util.http.fileupload.FileItemFactory;
import org.apache.tomcat.util.http.fileupload.FileUploadException;
import org.apache.tomcat.util.http.fileupload.disk.DiskFileItemFactory;
import org.apache.tomcat.util.http.fileupload.servlet.ServletFileUpload;
import org.apache.tomcat.util.http.fileupload.servlet.ServletRequestContext;
import org.json.JSONException;
import org.json.JSONObject;

import com.face.common.jsu.Rename;
import com.face.operate.jsu.Prestore;

//在集合中新增照片
@WebServlet("/AddPrestore")
public class AddPrestore extends HttpServlet {

	private static final long serialVersionUID = 1L;
	private ServletFileUpload upload;
	private final long MAXSize = 4194304 * 2L;// 4*2MB
	private String saveDir = "/root/prestore/";
	
	public void init() throws ServletException {
		FileItemFactory factory = new DiskFileItemFactory();
		this.upload = new ServletFileUpload(factory);
		this.upload.setSizeMax(this.MAXSize);
		this.upload.setHeaderEncoding("UTF-8");
	}

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		this.doPost(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		String filename = "";
		String filepath = "";
		
		Map<String, String> map = new HashMap<>();
		Map<String, String> fileMap = new HashMap<>();
		try {
			List<FileItem> items = this.upload
					.parseRequest(new ServletRequestContext(request));
			if (items != null && !items.isEmpty()) {
				for (FileItem fileItem : items) {
					if (fileItem.isFormField()) {
						String inputName = fileItem.getFieldName();
						String inputValue = new String(
								fileItem.getString("UTF-8"));
						map.put(inputName, inputValue);
						System.out.println(inputName + ":" + inputValue);
					} else {
						filename = Rename.rename();
						filepath = saveDir + filename;
						System.out.println("文件保存路径为:" + filepath);
						File file = new File(filepath);
						InputStream inputSteam = fileItem.getInputStream();
						BufferedInputStream fis = new BufferedInputStream(
								inputSteam);
						FileOutputStream fos = new FileOutputStream(file);
						int f;
						while ((f = fis.read()) != -1) {
							fos.write(f);
						}
						fos.flush();
						fos.close();
						fis.close();
						inputSteam.close();
						fileMap.put(filename, filepath);
					}
				}
			}
		} catch (FileUploadException e) {
			e.printStackTrace();
			System.out.println("上传失败");
		}
        Prestore pre = new Prestore();
		for (Object obj : fileMap.keySet()) {

			filename = (String) obj;
			filepath = fileMap.get(filename);
            pre.add(map.get("id"),map.get("phone_num"),filename);
		}
		
		JSONObject json = new JSONObject();
		try {
			json.put("message", "ok");
		} catch (JSONException e) {
			e.printStackTrace();
		}
		response.setContentType("application/json; charset=utf-8");
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		out.write(json.toString());
		System.out.println("返回的json数据：" + json.toString());
	}

}
